#include "qprojectmodel.h"
#include "../../11-util/02-log_manager/logmanager.h"
QProjectModel::QProjectModel(QObject *parent)
    : QAbstractItemModel(parent)
{

    ProjectModel * m = new ProjectModel();
    m->title = "root";
    rootItem = m;

}

QVariant QProjectModel::headerData(int section, Qt::Orientation orientation, int role) const
{
    // FIXME: Implement me!
    return QVariant();
}


/**
 *
 * 创建数据索引
 * @brief QProjectModel::index
 * @param row
 * @param column
 * @param parent
 * @return
 */
QModelIndex QProjectModel::index(int row, int column, const QModelIndex &parent) const
{
    //parent无效,返回root的索引.
    if (parent.isValid() && parent.column() != 0)
        return QModelIndex();

    //根据父索引获取父item
    ProjectModel *parentItem = getItem(parent);

    //无父item,返回root索引.
    if (!parentItem)
        return QModelIndex();

    ProjectModel *childItem = parentItem->getSub(row);
    if (childItem)
        return createIndex(row, column, childItem);
    return QModelIndex();
}

/**
 * 获取一个索引的父节点的索引
 * @brief QProjectModel::parent
 * @param index
 * @return
 */
QModelIndex QProjectModel::parent(const QModelIndex &index) const
{
    if (!index.isValid())
        return QModelIndex();

    ProjectModel *childItem = getItem(index);
    ProjectModel *parentItem = childItem ? childItem->parent() : nullptr;

    if (parentItem == rootItem || !parentItem)
        return QModelIndex();

    return createIndex(parentItem->subsCount(), 0, parentItem);

}


/*
 * 返回当前parent下有多少个子节点.
 *
 *
 */
int QProjectModel::rowCount(const QModelIndex &parent) const
{
    int ret = 0;
    if (parent.isValid() && parent.column() > 0){
    }
    else {
    const ProjectModel *parentItem = parent.isValid()
                                     ? static_cast<const ProjectModel*>(parent.internalPointer())
                                     : rootItem;

    ret = parentItem->subsCount();
    }
    QString info = QString("row %1 count %2").arg(parent.row(),ret);
    LogManager::debug(info);

    return ret;

    // FIXME: Implement me!
}

/**
 * step1,先实现在root中添加
 * root对应的Index是 QModelIndex();
 * @brief QProjectModel::appendRow
 * @param model
 */
void QProjectModel::appendRow(ProjectModel *model) {
    int count = this->rootItem->subsCount();
    LogManager::debug("appendRow :" + QString::number(count));
    //通知view要添加数据了,
    beginInsertRows(QModelIndex(),count,1);
    this->rootItem->addSub(model);
    endInsertRows();
}

int QProjectModel::columnCount(const QModelIndex &parent) const
{
    return 1;

    // FIXME: Implement me!
}

/**
 * 返回索引位置的数据
 * @brief QProjectModel::data
 * @param index
 * @param role
 * @return
 */
QVariant QProjectModel::data(const QModelIndex &index, int role) const
{
    if (!index.isValid() || role != Qt::DisplayRole)
        return {};

    const ProjectModel *item = static_cast<const ProjectModel*>(index.internalPointer());
    return item->title;
}


/**
 * 更新已有位置上的数据.
 * @brief QProjectModel::setData
 * @param index
 * @param value
 * @param role
 * @return
 */
bool QProjectModel::setData(const QModelIndex &index,  ProjectModel &value, int role)  {
    auto m = std::make_unique<ProjectModel>();
    m->title = value.title;
    rootItem->addSub2(std::move(m));
    rootItem->addSub(&value);

    return true;
}

/**
 * 根据索引得到对应的model.
 * index内部包含了model的引用.
 * @brief QProjectModel::getItem
 * @param index
 * @return
 */
ProjectModel *QProjectModel::getItem(const QModelIndex &index) const {
    if (index.isValid()) {
        ProjectModel *item = static_cast<ProjectModel*>(index.internalPointer());
        if (item)
            return item;
    }
    return rootItem;
}
